var express = require("express");
var router = express.Router();

const {
	login2,
	addnewadmin2,
	getadminlist2,
	changeavtive2,
} = require("../service/adminService");
//登录
router.post("/login", async (req, res, next) => {
	const data = await login2(req.body);
	if (data.length > 0 && data[0].active) {
		req.session.currentadmin = data[0].account;
		res.send(true);
	} else {
		res.send(false);
	}
});
//验证是否登录
router.get("/islogin", (req, res, next) => {
	const { currentadmin } = req.session;
	if (currentadmin) {
		res.send({ islogin: true, currentadmin });
	} else {
		res.send({
			islogin: false,
		});
	}
});

//注销
router.get("/logout", function (req, res, next) {
	req.session.destroy(function () {
		res.clearCookie("currentUser");
		res.send(true);
	});
});
//添加新管理员
router.post("/addnewadmin", async (req, res, next) => {
	res.send(await addnewadmin2(req.body));
});

//得到管理员列表
router.get("/getadminlist", async (req, res, next) => {
	res.send(await getadminlist2());
});
//修改管理员状态
router.put("/changeactive", async (req, res, next) => {
	res.send(await changeavtive2(req.body));
});

module.exports = router;
